#include<stdio.h>
#include<stdlib.h>
#include<gui.h>
#include<gui_list.h>
#include<assert.h>
#include"fm_list.h"
#include"fm_window.h"
#include"fm_func.h"

gui_spirit_t *fm_list=NULL;
char cur_file_name[64];
char cur_file_type[12];

void fm_list_create(int lists,int width,int height)
{
    gui_spirit_t *spirit=gui_list_create_no_padding(lists,width,height,width,gui_text_char_height()+3);
    if(!spirit)
    {
        dprintf("[fm] file list create error\n");
        return 0;
    }
    dprintf("[fm] gui list create sucessful\n");

    gui_list_set_pos(spirit,0,FM_FUNC_BUTTON_SIZE_H+1);

    gui_container_add(GUI_CONTAINER(fm_window),spirit);

    gui_spirit_show_children(spirit);
    
    fm_list=spirit;
    assert(fm_list);

    //fm_dir_open("/userapp");
    //while(1);
}

void fm_list_destroy()
{
    gui_spirit_t *spirit=fm_list;
    assert(fm_list);
     gui_spirit_destroy_all(spirit);
     gui_spirit_hide_all(spirit);
}

char *fm_list_get_file_type(int list_idx)
{
    char *p=gui_list_get_item_text(GUI_LIST(fm_list),list_idx);
    if(!p||!p[0])
        return NULL;

    char *type=cur_file_type;
    
    //skip name
    while(*p!=' ') 
        p++;
    p++;

    while(*p!=' ')
        *type++=*p++;
    *type='\0';

    dprintf("[fm] debug: get file type %s\n",cur_file_type);
    return cur_file_type;
}

char * fm_list_get_file_name(int list_idx)
{
    char *p=gui_list_get_item_text(GUI_LIST(fm_list),list_idx);
    if(!p||!p[0])
        return NULL;
    char *name=cur_file_name;
   
        while(*p!=' ')
        {
            *name++=*p++;
        }
        *name='\0';
    
    dprintf("[fm] debug: get file name %s\n",cur_file_name);
    return cur_file_name;
}


static int fm_list_button_down(gui_spirit_t *spirit,int idx)
{
    dprintf("button down on spirit %x text %s\n",spirit,spirit->text);
    char *filename=fm_list_get_file_name(idx);
    if(!filename)
    {
        dprintf("[fm] button_down: get file name err\n");
        return -1;
    }
    //dprintf("[fm] get file name %s\n",filename);

    char *filetype=fm_list_get_file_type(idx);
    if(!filetype)
    {
        dprintf("[fm] button down: get file type err\n");
        return -1;
    }
    //dprintf("[fm] get file type %s\n",filetype);

    if(!strcmp(filetype,"director"))
    {
        fm_list_clear();
        fm_dir_open(filename);
        char path[MAX_PATH_LEN];
        buildpath(filename,path);
        if(chdir(path)<0) //change work directory
            return ;
        //insert path record
        fm_records_set(path);
        //update current work path
        fm_label_update(path);
    }
}

void fm_list_clear()
{
    gui_spirit_t *spirit=fm_list;
    assert(spirit);

    gui_spirit_hide(spirit);
    for(int i=0;i<GUI_LIST(spirit)->list_num;i++)
    {
        gui_spirit_set_text(GUI_LIST(spirit)->buttons[i]," ");
    }
    //gui_spirit_show_all(spirit);
}

void fm_list_show_name(int list_idx,char *name,char *type,char *date,char *size)
{
    char buff[128];

    snprintf(buff,128,"%s %s %s %s",name,type,date,size);
    if(list_idx<GUI_LIST(fm_list)->list_num) //prevent overflow 
    {
        gui_list_set_item_text(fm_list,list_idx,buff,50);
        gui_signal_connect(GUI_LIST(fm_list)->buttons[list_idx],"button_press",fm_list_button_down,list_idx);
    }
}


void fm_list_show()
{
    gui_spirit_show_all(fm_list);
}

